package com.mak001.lite.multiSkill.subs.Miners;

import org.rsbuddy.tabs.Inventory;
import org.rsbuddy.widgets.Bank;

import extras.mak001.AnimWatcher;
import com.mak001.lite.multiSkill.Methods;
import extras.mak001.Time;
import com.mak001.lite.multiSkill.subScript;
import com.rsbuddy.event.events.MessageEvent;
import com.rsbuddy.script.methods.Calculations;
import com.rsbuddy.script.methods.Npcs;
import com.rsbuddy.script.methods.Objects;
import com.rsbuddy.script.methods.Players;
import com.rsbuddy.script.methods.Walking;
import com.rsbuddy.script.wrappers.Area;
import com.rsbuddy.script.wrappers.GameObject;
import com.rsbuddy.script.wrappers.Npc;
import com.rsbuddy.script.wrappers.Tile;

public class RuneEssMiner extends subScript {

	Area bankArea;
	Area teleArea = new Area(new Tile(3249, 3398), new Tile(3256, 3405));
	Area varrock = new Area(new Tile(3249, 3398), new Tile(3256, 3405));
	boolean isVarrock;

	boolean switchB;

	int pick = 1275;
	int[] person = { 5913, 300 };
	int[] closedDoor = { 33060, 11993, 24831 };
	Tile[] doorTiles = { new Tile(3107, 9570), new Tile(3107, 3162),
			new Tile(3109, 3167), new Tile(3253, 3399) };

	private int getX(Tile t) {
		return t.getX();
	}

	private int getY(Tile t) {
		return t.getY();
	}

	private boolean isSameTile(Tile t1, Tile t2) {
		return getX(t1) == getX(t2) && getY(t1) == getY(t2);
	}

	private boolean isSameArea(Area a1, Area a2) {
		return isSameTile(a1.getCentralTile(), a2.getCentralTile());
	}

	private enum State {
		TOBANK, MINE, TOMINE, PORTAL, TELE, BANK
	};

	private State getState() {
		if (!Inventory.isFull()) {
			if (Objects.getNearest(essenceID()) != null) {
				return State.MINE;
			} else {
				if (Npcs.getNearest(person) != null) {
					if (Npcs.getNearest(person).getLocation().isOnScreen()) {
						return State.TELE;
					} else {
						return State.TOMINE;
					}
				} else {
					return State.TOMINE;
				}
			}
		} else {
			if (!bankArea.contains(getLocation())) {
				if (Objects.getNearest(essenceID()) == null) {
					return State.TOBANK;
				} else {
					return State.PORTAL;
				}
			} else {
				return State.BANK;
			}
		}
	}

	private Tile getLocation() {
		return Players.getLocal().getLocation();
	}

	private int essenceID() {
		int ESSENCE_ROCK = 2491;
		return ESSENCE_ROCK;
	}

	public void setSwitch(boolean b) {
		if (switchB != b)
			switchB = b;
	}

	private Npc portalID() {
		int p[] = { 13628, 13629, 13630, 13631 };
		int nearest = 0;
		for (int i = 0; i < p.length; i++) {
			if (nearest == 0) {
				if (Npcs.getNearest(p[i]) != null)
					nearest = p[i];
			} else {
				if (Npcs.getNearest(p[i]) != null)
					if (Calculations.distanceTo(Npcs.getNearest(p[i])) < Calculations
							.distanceTo(Npcs.getNearest(nearest)))
						nearest = p[i];
			}
		}
		return Npcs.getNearest(nearest);
	}

	@Override
	public void execute() {
		if (bankArea == null) {
			isVarrock = isSameArea(varrock, teleArea);
			bankArea = Methods.getNearestBank(teleArea.getCentralTile());

		}
		switch (getState()) {
		case BANK:
			setSwitch(true);
			if (Bank.isOpen()) {
				Bank.depositAllExcept(pick);
			} else {
				Bank.open();
			}
			break;
		case TOMINE:
			if (Bank.isOpen()) {
				Bank.close();
			} else {
				if (Objects.getNearest(closedDoor) != null) {
					if (tileEquals(
							Objects.getNearest(closedDoor).getLocation(),
							doorTiles)) {
						if (Objects.getNearest(closedDoor).interact("")) {
							sleep(500);
						}
					}
				} else {
					if (isVarrock) {
						Walking.findPath(teleArea.getCentralTile()).traverse();
					} else {
						if (teleArea.getCentralTile().isOnMap()) {
							Walking.findPath(teleArea.getCentralTile())
									.traverse();
						} else {
							Tile t = new Tile(3105, 3162);
							GameObject stairs = Objects.getNearest(2147);
							if (stairs == null) {
								Walking.findPath(t).traverse();
							} else {
								if (stairs != null) {
									if (stairs.isOnScreen()) {
										stairs.interact("");
									} else {
										Walking.findPath(t).traverse();
									}
								}
							}
						}
					}
				}
			}
			break;
		case TOBANK:
			if (Objects.getNearest(closedDoor) != null) {
				if (tileEquals(Objects.getNearest(closedDoor).getLocation(),
						doorTiles)) {
					if (Objects.getNearest(closedDoor).interact("")) {
						sleep(500);
					}
				}
			} else {
				if (isVarrock) {
					Walking.findPath(bankArea.getCentralTile()).traverse();
				} else {
					GameObject stairs = Objects.getNearest(32015);
					if (stairs != null) {
						if (stairs.getLocation().isOnMap()) {
							if (stairs.isOnScreen()) {
								stairs.interact("");
							} else {
								Walking.findPath(stairs.getLocation())
										.traverse();
							}
						}
					} else {
						Walking.findPath(bankArea.getCentralTile()).traverse();
					}
				}
			}
			break;
		case TELE:
			setSwitch(false);
			if (!Players.getLocal().isMoving()
					&& Players.getLocal().getAnimation() == -1
					&& Players.getLocal().getInteracting() == null) {
				if (Npcs.getNearest(person).interact("Teleport")) {
					sleep(1000);
				}
			}
			break;
		case PORTAL:
			if (portalID() != null)
				if (portalID().getLocation().isOnScreen()) {
					if (!Players.getLocal().isMoving()
							&& Players.getLocal().getAnimation() == -1) {
						if (portalID().interact("")) {
							sleep(1000);
						}
					}
				} else {
					Walking.findPath(portalID().getLocation()).traverse();
				}
			break;
		case MINE:
			if (!Players.getLocal().isMoving() && AnimWatcher.isIdle()) {
				if (Objects.getNearest(essenceID()).getLocation().isOnScreen()) {
					if (Objects.getNearest(essenceID()).interact("")) {
						sleep(500);
					}
				} else {
					Walking.findPath(
							Objects.getNearest(essenceID()).getLocation())
							.traverse();
				}
			} else {
				sleep(500);
			}
			break;
		}
	}

	private boolean tileEquals(Tile l, Tile[] d) {
		boolean b = false;
		for (Tile t : d) {
			if (l.equals(t)) {
				if (b != true)
					b = true;
			}
		}
		return b;
	}

	@Override
	public int[] required() {
		int[] req = { pick };
		// TODO Auto-generated method stub
		return req;
	}

	@Override
	public boolean canSwitch() {
		return switchB;
	}

	public void setPlace(Area place) {
		teleArea = place;
	}

	@Override
	public String getStatus() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int skill() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void messageRecieved(MessageEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String[] paintString() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Time getTime() {
		// TODO Auto-generated method stub
		return null;
	}

}
